//
//  RBGoodsDetailMultipleChoosePopVC.m
//  zhongzhenglu
//
//  Created by mac on 2022/11/25.
//

#import "RBGoodsDetailMultipleChoosePopVC.h"
#import "RBDefine.h"
#import "UIView+RBAdd.h"
#import "UIScrollView+RBAdd.h"
#import <Masonry.h>
#import "RBThemeConfig.h"
#import <UIColor+YYAdd.h>
#import <UIView+YYAdd.h>
#import "RanTool.h"
#import "RBTagContainerView.h"

@interface RBGoodsDetailMultipleChoosePopVC ()

@property(nonatomic, strong) NSMutableArray *tagViewsArrayM;

@end

@implementation RBGoodsDetailMultipleChoosePopVC

#pragma mark - Lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setup];
}

#pragma mark - Private

- (void)setup
{
    self.view.backgroundColor = UIColor.clearColor;
    
    self.contentView = ({
        UIView *view = UIView.new;
        [self.view addSubview:view];
        view.backgroundColor = UIColor.whiteColor;
        [view rbSetCorner:UIRectCornerTopLeft | UIRectCornerTopRight radius:Zoom(8)];
        
        view;
    });
    
    CGFloat totalH = Zoom(16);
    [self.contentView view_addView:({
        UIView *view = UIView.new;
        [view view_addView:[RanTool labelWithFontSize:Zoom(17) weight:1 color:[RBThemeConfig textBlackColor] align:1 text:self.title] withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:0 bottom:0 left:0 right:0 centerX:1];
        view;
    }) withHeight:Zoom(30) width:CGFLOAT_MIN top:totalH bottom:CGFLOAT_MIN left:Zoom(17) right:Zoom(17) centerX:NO];
    totalH += Zoom(30);
    
    CGFloat bottomH = MAX(Zoom(40), kRBSafeBottomHeight);
    totalH += bottomH;
    
    CGFloat topPadding = Zoom(15);
    totalH += topPadding;
    
    [self.contentView view_addView:({
        UIScrollView *view = UIScrollView.new;
        view.showsVerticalScrollIndicator = NO;
        view.showsHorizontalScrollIndicator = NO;
        [view rbSetContentWidth:kRBMainScreenWidth - Zoom(15)*2];
        
        RBTagContainerView *tagContainer = RBTagContainerView.new;
        tagContainer.constraintWidth = kRBMainScreenWidth - Zoom(15)*2;
        tagContainer.hMargin = Zoom(8);
        tagContainer.vMargin = Zoom(10);
        [tagContainer addTagViews:({
            NSMutableArray *arrayM = NSMutableArray.array;
            for (NSInteger i = 0; i < self.options.count; i++)
            {
                RBGoodsDetailSpecItemModel *model = self.options[i];
                [arrayM addObject:({
                    UILabel *view = UILabel.new;
                    view.rbExData = model;
                    [self reloadItemView:view];
                    [view rbAddTapGesWithTarget:self action:@selector(itemViewTapped:)];
                    view.text = model.spec_name;
                    view.font = [UIFont systemFontOfSize:Zoom(11)];
                    view.size = CGSizeMake(Zoom(79), Zoom(25));
                    view.textAlignment = NSTextAlignmentCenter;
                    view.textColor = [UIColor colorWithHexString:@"#333333"];
                    [self.tagViewsArrayM addObject:view];
                    [view rbSetCornerRadius:Zoom(12.5)];
                    view;
                })];
            }
            arrayM;
        })];
        [view addView:tagContainer withHeight:tagContainer.calulatedHeight width:CGFLOAT_MIN top:0 bottom:0 left:0 right:0 centerX:0];
        totalH += tagContainer.calulatedHeight;
        
        view;
    }) withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:topPadding bottom:bottomH left:Zoom(15) right:Zoom(15) centerX:0];
    
    totalH = MIN(totalH, kRBMainScreenHeight * 0.7);
    self.contentView.frame = CGRectMake(0, 0, kRBMainScreenWidth, totalH);
    self.contentView.top = kRBMainScreenHeight;
    
    UIImageView *closeImgView = [RanTool imgViewWithMode:1 radius:0 imgName:@"goodsDetailPramsClose"];
    [closeImgView rbAddTapGesWithTarget:self action:@selector(dismiss)];
    [closeImgView extendResponseArea];
    [self.contentView addSubview:closeImgView];
    [closeImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(Zoom(22));
        make.right.mas_equalTo(-Zoom(16));
        make.width.height.mas_equalTo(Zoom(18));
    }];
}

- (void)show
{
    [UIView animateWithDuration:0.15 animations:^{
        self.view.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
        self.contentView.bottom = kRBMainScreenHeight;
    }];
}

- (void)dismiss
{
    if(self.dismissBlock)
    {
        self.dismissBlock();
    }
    [UIView animateWithDuration:0.15 animations:^{
        self.view.backgroundColor = UIColor.clearColor;
        self.contentView.top = kRBMainScreenHeight;
    } completion:^(BOOL finished) {
        [self dismissViewControllerAnimated:NO completion:nil];
    }];
}

- (void)reloadItemView: (UIView *)view
{
    RBGoodsDetailSpecItemModel *model = view.rbExData;
    view.backgroundColor = !model.selected? [UIColor colorWithHexString:@"#F2F2F2"]: [UIColor colorWithHexString:@"#C5D5FF"];
}

#pragma mark - Network

#pragma mark - Event

- (void)itemViewTapped: (UITapGestureRecognizer *)ges
{
    RBGoodsDetailSpecItemModel *model = ges.view.rbExData;
    if(!self.singleSelect || model.selected)
    {
        model.selected = !model.selected;
        [self reloadItemView:ges.view];
    }
    else
    {
        for (RBGoodsDetailSpecItemModel *item in self.options)
        {
            item.selected = item == model;
        }
        for (UIView *view in self.tagViewsArrayM)
        {
            [self reloadItemView:view];
        }
    }
}

#pragma mark - Setter

#pragma mark - Getter

- (NSMutableArray *)tagViewsArrayM
{
    if(!_tagViewsArrayM)
    {
        self.tagViewsArrayM = NSMutableArray.array;
    }
    return _tagViewsArrayM;
}

@end
